package club.kunlingou.dsc.blog.controller;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import club.kunlingou.dsc.blog.domain.ArticleInfo;
import club.kunlingou.dsc.blog.service.ArticleInfoService;
import club.kunlingou.dsc.common.web.WebResponse;

@Controller
@RequestMapping("blog/article")
public class ArticleController {
	
	@Autowired
	ArticleInfoService articleInfoService;
	
	@RequestMapping("view/{id}")
	@ResponseBody
	public JSONObject view(@PathVariable Long id) {
		ArticleInfo article = articleInfoService.get(id);
		return WebResponse.success(article).toJSON();
	}
	
	@RequestMapping("list")
	@ResponseBody
	public JSONObject list(@RequestBody ArticleInfo query) {
		List<ArticleInfo> articles = articleInfoService.list(query);
		return WebResponse.success(articles).toJSON();
	}
	
	@RequestMapping("save")
	@ResponseBody
	public JSONObject save(@RequestBody ArticleInfo model) {
		if(articleInfoService.save(model)) {
			return WebResponse.success().toJSON();
		}else {
			return WebResponse.fail().toJSON();
		}
	}
	
}
